package com.example.simple.work.service.impl;

import com.example.simple.work.entity.EmployeeMessage;
import com.example.simple.work.mapper.EmployeeMapper;
import com.example.simple.work.service.IEmployeeService;
import com.example.simple.work.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 插入一条员工信息数据--重构
     * */
    @Override
    public void insert(EmployeeMessage employeeMessage) {
        String employeeId = employeeMessage.getEmployeeId();
        if(employeeId == null || employeeId.equals("")){
            throw new EmployeeIdNotNullException("员工工号不能为空");
        }
        EmployeeMessage result = employeeMapper.findByEmployeeId(employeeId);
        System.out.println("000"+result);
        if(result != null){
            System.out.println("000"+result);
            throw new EmployeeIdDuplicatedException("员工工号已存在，请勿重复插入");
        }
        // 补全数据
        employeeMessage.setIsDelete(0);
        employeeMessage.setCreateUser("崔阳亮");
        employeeMessage.setModifiedUser("崔阳亮");
        Date date = new Date();
        employeeMessage.setCreateTime(date);
        employeeMessage.setModifiedTime(date);

        Integer rows = employeeMapper.insert(employeeMessage);
        if(rows != 1){
            throw new InsertException("员工信息输入时存在未知错误");
        }
    }

    /**
     * 根据员工工号删除员工信息--重构
     * */
    @Override
    public void delete(String employeeId) {
        EmployeeMessage result = employeeMapper.findByEmployeeId(employeeId);
        if(result == null){
            throw new EmployeeIdNotExistException("员工工号不存在");
        }
        Integer rows = employeeMapper.updateIsDelete(employeeId);
        if (rows != 1){
            throw new EmployeeDeleteException("删除员工信息时出现未知错误");
        }
    }

    /**
     * 查询所有员工的数据--重构
     * */
    @Override
    public List<EmployeeMessage> findAllEmployeeMessageData() {
        List<EmployeeMessage> list = employeeMapper.findAllEmployeeMessageData();
        if(list == null){
            throw new EmployeeFindAllException("查询所有员工信息时遇见未知错误");
        }
        return list;
    }

    @Override
    public List<EmployeeMessage> findMessageByEmployeeName(String employeeName) {
        List<EmployeeMessage> list = employeeMapper.findMessageByEmployeeName(employeeName);
        if(list == null){
            throw new EmployeeFindAllException("查询所有员工信息时遇见未知错误");
        }
        return list;
    }

//    /**
//     * 根据员工工号查询员工信息--重构
//     * */
//    @Override
//    public EmployeeMessage findEmployeeMessageByEmployeeId(String employeeId) {
//        EmployeeMessage result = employeeMapper.findByEmployeeId(employeeId);
//        if(result == null){
//            throw new EmployeeIdNotExistException("员工工号不存在");
//        }
//        return result;
//    }

    /**
     * 根据员工工号修改员工信息
     * */
    @Override
    public void changeEmployee(String employeeId, EmployeeMessage employeeMessage) {
        EmployeeMessage result = employeeMapper.findByEmployeeId(employeeId);
        if(result == null){
            throw new EmployeeIdNotExistException("员工工号不存在");
        }

        // 更新修改时间
        Date date = new Date();
        employeeMessage.setModifiedTime(date);

        Integer rows = employeeMapper.update(employeeId,employeeMessage);
        if (rows != 1){
            throw new EmployeeChangeException("修改员工信息时出现未知错误");
        }
    }

    @Override
    public void insertList(List<EmployeeMessage> employeeMessageList) {
        if(employeeMessageList==null || employeeMessageList.isEmpty()){
            throw new EmployeeInsertListIsNotExistException("数据不能为空");
        }
        for (EmployeeMessage employeeMessage: employeeMessageList){
            employeeMessage.setIsDelete(0);
            employeeMessage.setCreateUser("崔阳亮");
            employeeMessage.setModifiedUser("崔阳亮");
            Date date = new Date();
            employeeMessage.setCreateTime(date);
            employeeMessage.setModifiedTime(date);
        }
        Integer rows = employeeMapper.insertList(employeeMessageList);
        if(rows < 1){
            throw new EmployeeInsertListException("批量插入数据时出现异常");
        }
    }


}
